package jianzhioffer;
// 剑指 Offer 05. 替换空格
public class ReplaceSpace {


    public String replaceSpace(String s) {
        char[] chars = s.toCharArray();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i <chars.length; i++) {
            if (chars[i]==' '){
                stringBuffer.append('%');
                stringBuffer.append('2');
                stringBuffer.append('0');
            }else {
                stringBuffer.append(chars[i]);
            }
        }
        return stringBuffer.toString();
    }


    public String replaceSpace2(String s) {
        char[] chars = s.toCharArray();
        int index=0;
        // 预开辟
        char[] res = new char[chars.length*3];
        for (int i = 0; i < chars.length; i++) {
            if (chars[i]==' '){
                res[index++] = '%';
                res[index++] = '2';
                res[index++] = '0';

            }else {
                res[index++] = chars[i];
            }
        }
        return new String(res).substring(0,index);
    }
}
